<template>
	<div class="mainbody">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>系统管理</el-breadcrumb-item>
				<el-breadcrumb-item>管理员列表</el-breadcrumb-item>
			</el-breadcrumb>
		</div>

		<div class="toolbar-box">
			<div class="list-box">
				<div class="l-list">
					<el-button-group>
						<el-button icon="el-icon-plus" @click="$common.linkUrl('/manager/edit')">新增</el-button>
						<el-button icon="el-icon-delete" @click="deleteCheckAll">删除</el-button>
					</el-button-group>
				</div>
				<div class="r-list">
					<div class="search-box">
						<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initData" clearable>
							<el-button slot="append" icon="el-icon-search" @click="initData"></el-button>
						</el-input>
					</div>
				</div>
			</div>

		</div>

		<div class="content-box">
			<el-card class="table-card">
				<el-table ref="multipleTable" v-loading="loading" :data="listData" stripe class="table-list" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="45"></el-table-column>
					<el-table-column prop="userName" label="用户名" min-width="100"></el-table-column>
					<el-table-column prop="email" label="邮箱" min-width="150"></el-table-column>
					<el-table-column prop="phone" label="手机" min-width="110"></el-table-column>
					<el-table-column prop="realName" label="姓名" width="100"></el-table-column>
					<el-table-column prop="status" label="状态" width="90" align="center">
						<template slot-scope="scope">
							<el-tag size="small" type="success" effect="dark" v-if="scope.row.status==0">正常</el-tag>
							<el-tag size="small" type="warning" effect="dark" v-else-if="scope.row.status==1">待验证</el-tag>
							<el-tag size="small" type="warning" effect="dark" v-else-if="scope.row.status==2">待审核</el-tag>
							<el-tag size="small" type="info" effect="dark" v-else>黑名单</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="addTime" label="创建时间" width="160"></el-table-column>
					<el-table-column fixed="right" label="操作" width="90">
						<template slot-scope="scope">
							<el-button size="mini" icon="el-icon-edit" @click="$common.linkUrl('/manager/edit/'+scope.row.userId)"></el-button>
							<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteItem(scope.row.userId)"></el-button>
						</template>
					</el-table-column>
				</el-table>
			</el-card>

			<div class="pager-box">
				<el-pagination background
							   @size-change="handleSizeChange"
							   @current-change="handleCurrentChange"
							   :current-page="pageIndex"
							   :page-sizes="[10, 20, 50, 100]"
							   :page-size="pageSize"
							   layout="total, sizes, prev, pager, next, jumper"
							   :total="totalCount">
				</el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				keyword: '',
				totalCount: 0,
				pageSize: 10,
				pageIndex: 1,
				listData: [],
				multipleSelection: [],
			}
		},
		methods: {
			//加载数据
			initData() {
				let _this = this;
				let sendUrl = "/admin/manager";
				sendUrl += "?pageSize=" + _this.pageSize;
				sendUrl += "&pageIndex=" + _this.pageIndex;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.listData = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
					},
					error(err) {
						_this.listData = null
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//多选删除
			deleteCheckAll() {
				let _this = this; //当前页面
				//拿到选中的数据
				let list = _this.multipleSelection;
				//检查是否有选中
				if (!list.length) {
					_this.$message({ type: 'warning', message: '请选择要删除的记录！' });
					return false;
				}
				//执行删除操作
				_this.$confirm('确认要删除该记录吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function () {
					let delIds = [];//创建一个数组
					list.forEach((item, index) => {
						//遍历源数据
						_this.listData.forEach((v, i) => {
							if (item.id == v.id) {
								delIds.push(item.userId);
							}
						})
					});
					_this.$api.request({
						method: 'delete',
						url: '/admin/manager?ids=' + delIds.toString(),
						loading: true,
						successMsg: '已删除成功',
						success(res) {
							_this.initData(); //重新加载列表
							//清除选中状态
							_this.$refs.multipleTable.clearSelection();
						}
					})
				}).catch(function () { });
			},
			//单项删除
			deleteItem(val) {
				let _this = this; //当前页面
				//执行删除操作
				_this.$confirm('确认要删除该记录吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function () {
					_this.$api.request({
						method: 'delete',
						url: '/admin/manager/' + val,
						loading: true,
						successMsg: '已删除成功',
						success(res) {
							_this.initData();//重新加载列表
						}
					});
				}).catch(function () { });
						
			},
			//选中第几行
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			//每页显示数量
			handleSizeChange(val) {
				if (this.pageSize != val) {
					this.pageSize = val;
					this.initData();
				}
			},
			//跳转到第几页
			handleCurrentChange(val) {
				if (this.pageIndex != val) {
					this.pageIndex = val;
					this.initData();
				}
			},
		},
		created() {
			this.initData();
		}
	}
</script>

<style lang="scss">
</style>